package unit2.csvPaser;

import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.Reader;
import java.util.ArrayList;
import java.util.List;

import unit2.question.ChooseAnswerQuestion;

import com.googlecode.jcsv.reader.CSVEntryParser;
import com.googlecode.jcsv.reader.CSVReader;
import com.googlecode.jcsv.reader.internal.CSVReaderBuilder;



public  class ChooseAnswerQuestionEntryParser implements CSVEntryParser<ChooseAnswerQuestion> {
	  @Override
	  public ChooseAnswerQuestion parseEntry(String... data) {
	    if (data.length != 2) {
	      throw new IllegalArgumentException("data is not a valid person record");
	    }
	                
	    String question = data[0];
	    String answer = data[1];
	                   
	    return  new ChooseAnswerQuestion (question, answer);
	  }
	  
	  public List<ChooseAnswerQuestion> readQuestions(String adressOfFile){
		  Reader reader = null;
		  	try {
				reader = new FileReader(adressOfFile);
			} catch (FileNotFoundException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}

			CSVReader<ChooseAnswerQuestion> csvQuestionReader = new CSVReaderBuilder<ChooseAnswerQuestion>(reader).entryParser(new ChooseAnswerQuestionEntryParser()).build();
		//	List<ChooseAnswerQuestion> 
			ArrayList<ChooseAnswerQuestion> questions = null;
			try {
				questions = (ArrayList<ChooseAnswerQuestion>) csvQuestionReader.readAll();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			return questions;
	  }
}